define(function(require, exports, module) {
    var baidu = function() {
        this.point = null;
        this.marker = null;
        this.infowindow = null;
        this.map = null

    }
    baidu.prototype.load = function() {
        if (typeof(BMap) == 'undefined') {
            var script = document.createElement("script");
            script.src = "http://api.map.baidu.com/api?v=2.0&ak=359042E5AC9ced07c553ebe2042aad73&callback=wm.map.maps.init";
            document.body.appendChild(script);
        } else {
            this.init();
        }
    };
    baidu.prototype.init = function() {
        var _self = this;
        this.map = new BMap.Map("l-map");
        this.point = new BMap.Point(wm.map.op.lng, wm.map.op.lat);
        this.map.centerAndZoom(this.point, 15);
        this.map.enableScrollWheelZoom();
        this.map.enableScrollWheelZoom();

        this.marker = new BMap.Marker(this.point);
        this.map.addOverlay(this.marker);



        this.map.addEventListener("dragend", function showInfo() {
            _self.marker.closeInfoWindow();
            _self.showAddress(_self.marker);
        });

        this.map.addEventListener("dragging", function showInfo() {
            var cp = _self.map.getCenter();
            _self.marker.setPosition(new BMap.Point(cp.lng, cp.lat));

        });
        this.marker.enableDragging();
        this.marker.addEventListener("click", function(e) {
            this.openInfoWindow(this.infoWindow);
        });
        this.marker.addEventListener("dragend", function(e) {
            _self.marker.closeInfoWindow();
            _self.showAddress(this.marker);
        });
        var opts = {
            width: 220,
            height: 80,
            title: "原本位置"
        };
        this.infoWindow = new BMap.InfoWindow(" " + wm.map.op.adr + " ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!", opts); // 创建信息窗口对象
        if (!wm.map.is_change) {
            this.marker.openInfoWindow(this.infoWindow);
        } else {
            this.search();
        }
        if (wm.map.is_default) {
            window.setTimeout(function() {
                _self.locate();
            }, 1000);
        }
    };


    baidu.prototype.locate = function() {
        var _self = this;
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                point = new BMap.Point(r.point.lng, r.point.lat);
                _self.marker.setPosition(point);
                _self.map.panTo(point);
                var opts = {
                    width: 220,
                    height: 60,
                    title: "定位成功"
                };
                _self.infoWindow = new BMap.InfoWindow("这是你当前的位置!,移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!", opts); // 创建信息窗口对象
                _self.marker.openInfoWindow(_self.infoWindow);
                _self.showAddress(_self.marker, true);

            } else {
                console.log("无法获取定位");
            }
        });
    };
    baidu.prototype.search = function() {
        var _self = this;
        var myGeo = new BMap.Geocoder();
        var addr = wm.map.el.suggestId.val();
        addr = addr.length > 0 ? addr : wm.map.op.adr;
        myGeo.getPoint(addr, function(point) {
            if (point) {
                _self.marker.setPosition(point);
                _self.map.panTo(point);
                var opts = {
                    width: 220,
                    height: 60,
                    title: "搜索位置"
                };
                _self.infoWindow = new BMap.InfoWindow("" + addr + ",移动红点标注目标位置，你也可以直接修改上方位置,系统自动定位!", opts); // 创建信息窗口对象
                _self.marker.openInfoWindow(_self.infoWindow); // 打开信息窗口
                // map.centerAndZoom(_self.marker.getPosition(), _self.map.getZoom());
                _self.showAddress(_self.marker, true);
            } else {
                console.log("Search no point");
            }
        }, "全国");
    };
    baidu.prototype.showAddress = function(marker, callout) {
        var latlng = marker.getPosition();
        var _self = this;
        var myGeo = new BMap.Geocoder();
        myGeo.getLocation(latlng, function(result) {
            if (result) {
                var el = wm.map.el;
                el.lat.val(latlng.lat);
                el.lng.val(latlng.lng);
                el.suggestId.val(result.address);
                if (!callout) {
                    var opts = {
                        width: 220,
                        height: 80,
                        title: "标注位置"
                    };
                    _self.infoWindow = new BMap.InfoWindow(" " + result.address + " ,拖拽地图或红点修改位置!你也可以直接修改上方位置系统自动定位!", opts);
                    //_self.marker.openInfoWindow(self.infoWindow);
                }

            } else {
                console.log("无法获取定位");
            }
        });
    };

    return baidu;
});
